package com.communal.web;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.communal.service.ICRService;
import com.communal.service.ITarifService;
import com.communal.util.AppException;
import com.communal.util.CommunalFilterBean;
import com.communal.util.SortPaginationBean;
import com.communal.web.form.cr.CRIntercomForm;
import com.communal.web.form.tarif.TarifIntercomForm;

/**
 * 
 * @author Created by andrey.kuznetsov <br>
 * @author Last modified by $Author$ <br>
 * @author Last modified on $Date$ at revision $Revision$ <br>
 */
@Controller
public class IntercomController extends AbstractController{

	private final String VIEW_ACTION = "/intercom/view.action";
	private final String UPDATE_ACTION = "/intercom/update.action";
	private final String DELETE_ACTION = "/intercom/delete.action";
	
	private final String VIEW_TARIF_ACTION = "/intercomTarif/view.action";
	private final String UPDATE_TARIF_ACTION = "/intercomTarif/update.action";
	private final String DELETE_TARIF_ACTION = "/intercomTarif/delete.action";
	
	@Autowired 
	ITarifService<TarifIntercomForm> tarifIntercomService;
	
	@Autowired
	ICRService<CRIntercomForm> intercomService;
	
	@RequestMapping(value=VIEW_ACTION)
	public @ResponseBody Map<String, ? extends Object> view(
			@RequestParam (value = "year", required = false) Short year,
			@RequestParam (value = "month", required = false) Short month,
			@RequestParam (value = "dir", required = false ) String dir,
			@RequestParam (value = "sort", required = false ) String sort){
		try{
			return getMap( intercomService.getRecords( 
					userService.getModelForCurrentLoggedUser(), new CommunalFilterBean( year, month, null, null ), new SortPaginationBean( sort, dir, null, null ) ) );
		} catch (AppException error) {
			return getModelMapError(error.getMessage());
		}
	}
	
	@RequestMapping(value=UPDATE_ACTION)
	public @ResponseBody Map<String, ? extends Object> update(@RequestParam Object data){
		try{
			return getMap( intercomService.saveOrUpdateRecord( data, userService.getModelForCurrentLoggedUser() ) );
		}catch (AppException error) {
			return getModelMapError(error.getMessage());
		}
	}
	
	@RequestMapping(value=DELETE_ACTION)
	public @ResponseBody Map<String, ? extends Object> delete(@RequestParam Object data) throws Exception {
		Map<String,Object> modelMap = null;
		try{
			intercomService.deleteRecords( data,  userService.getModelForCurrentLoggedUser() );
			modelMap = new HashMap<String,Object>(3);
			modelMap.put(KEY_SUCCESS, true);
			return modelMap;
		} catch (AppException error) {
			return getModelMapError(error.getMessage());
		}
	}
	
	@RequestMapping(value=VIEW_TARIF_ACTION)
	public @ResponseBody Map<String, ? extends Object> viewTarif(
			@RequestParam (value = "dir", required = false ) String dir,
			@RequestParam (value = "sort", required = false ) String sort){
		try{
			return getMap( tarifIntercomService.getRecords( userService.getModelForCurrentLoggedUser() ) );
		} catch (AppException error) {
			return getModelMapError(error.getMessage());
		}
	}
	
	@RequestMapping(value=UPDATE_TARIF_ACTION)
	public @ResponseBody Map<String, ? extends Object> updateTarif(@RequestParam Object data){
		try{
			return getMap( tarifIntercomService.saveOrUpdateRecord( data, userService.getModelForCurrentLoggedUser() ) );
		}catch (AppException error) {
			return getModelMapError(error.getMessage());
		}
	}
	
	@RequestMapping(value=DELETE_TARIF_ACTION)
	public @ResponseBody Map<String, ? extends Object> deleteTarif(@RequestParam Object data) throws Exception {
		Map<String,Object> modelMap = null;
		try{
			tarifIntercomService.deleteRecords( data, userService.getModelForCurrentLoggedUser() );
			modelMap = new HashMap<String,Object>(3);
			modelMap.put(KEY_SUCCESS, true);
			return modelMap;
		} catch (AppException error) {
			return getModelMapError(error.getMessage());
		}
	}
}
